import request from '@/utils/request';
export function createSupplier(requestData) {
    var paramRequestData = toFom(requestData);
    return request({
        url: '/v1/supplier/create',
        header: {
            'Content-Type': 'multipart/form-data;charset=utf-8'
        },
        method: 'post',
        data: paramRequestData
    });
}

export function getSupplierDetail(supplierId) {
    return request({
        url: `/v1/supplier/detail/${supplierId}`,
        method: 'get'
    });
}

export function getSupplierList(data) {
    return request({
        url: `/v1/supplier/list`,
        method: 'post',
        data: data
    });
}

export function checkNameExist(params) {
    return request({
        url: `/v1/supplier/name/exist`,
        method: 'get',
        params: params
    });
}

export function updateSupplier(params) {
    var paramRequestData = toFom(params);
    return request({
        url: `/v1/supplier/update`,
        header: {
            'Content-Type': 'multipart/form-data;charset=utf-8'
        },
        method: 'post',
        data: paramRequestData
    });
}

export function settlementList(params) {
    return request({
        url: ``,
        method: 'post',
        header: {
            'Content-Type': 'multipart/form-data;charset=utf-8'
        },
        data: params
    });
}

function toFom(params) {
    var paramRequestData = new FormData();
    var keys = Object.keys(params);
    keys.forEach(key => {
        if (params[key] instanceof Array) {
            let arr = params[key];
            arr.forEach((item, index) => {
                paramRequestData.append(`${key}[${index}]`, item);
            });
        } else paramRequestData.append(key, params[key]);
    });
    return paramRequestData;
}
